package com.texplose.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.texplose.entity.User;
import com.texplose.entity.constant.SystemConstants;
import com.texplose.entity.enums.AppHttpCodeEnum;
import com.texplose.entity.vo.ResponseResult;
import com.texplose.entity.vo.UserInfoVo;
import com.texplose.exception.SystemException;
import com.texplose.mapper.UserMapper;
import com.texplose.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : lyj
 * @Timer : 2022/5/17
 * @Description :
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult adminLogin(User user) {
        int type = user.getType();
        if (type == SystemConstants.IS_ADMIN) {
            userMapper.adminLogin(user);
            return ResponseResult.okResult();
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
    }

    @Override
    public ResponseResult login(User user) {
        userMapper.login(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getUserById(Integer userId) {
        User user = userMapper.getUserById(userId);
        return ResponseResult.okResult(user);
    }

    @Override
    public ResponseResult register(User user) {
        userMapper.register(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteUser(Integer userId) {
        userMapper.deleteUser(userId);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updatePasswordByEmail(User user) {
        userMapper.updatePasswordByEmail(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectAllUser() {
        userMapper.selectAllUser();
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult wxLogin(User user) {
        User loginUser = userMapper.wxLogin(user);
        if (loginUser != null) {
            return ResponseResult.okResult(loginUser);
        } else {
            throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);
            //return ResponseResult.errorResult(AppHttpCodeEnum.USERNAME_EXIST);
        }
    }

    @Override
    public ResponseResult wxRegister(User user) {
        Boolean isExist = checkUser(user);
        if (isExist) {
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        } else {
            userMapper.wxRegister(user);
            return ResponseResult.okResult("注册成功");
        }
    }


    @Override
    public ResponseResult getUserInfo(int id) {
        List<UserInfoVo> userInfo = userMapper.getUserInfo(id);
        return ResponseResult.okResult(userInfo);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAllUser();

        PageInfo<User> pageInfo = new PageInfo<>(users);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult updateAvatarById(Integer id, String avatarUrl) {
        userMapper.updateAvatarById(id, avatarUrl);
        return ResponseResult.okResult();
    }

    @Override
    public Boolean checkUser(User user) {
        Boolean checkUser = userMapper.checkUser(user);
        if (checkUser == null) {
            checkUser = false;
        }
        return checkUser;
    }

    @Override
    public ResponseResult updateUserInfo(UserInfoVo userInfo) {
        userMapper.updateUserInfo(userInfo);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult resetPwdByUserId(User user) {
        userMapper.resetPwdByUserId(user);
        return ResponseResult.okResult();
    }


}
